package com.arcsoft.commonbase.base

import android.app.Activity
import android.app.ActivityManager
import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.viewbinding.ViewBinding
import com.arcsoft.commonbase.R
import com.arcsoft.commonbase.bus.Bus
import com.arcsoft.commonbase.bus.bus_finish_load
import com.arcsoft.commonbase.widgets.ProgressDialogVer
import com.blankj.utilcode.util.BarUtils
import com.dylanc.viewbinding.base.ViewBindingUtil
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener


abstract class BaseBindingActivity<VB : ViewBinding> : AppCompatActivity() {

    lateinit var mActivity: Activity
    lateinit var binding: VB
    private var loadingView: ProgressDialogVer? = null

    //刷新布局
    var baseSmartRefreshLayout: SmartRefreshLayout? = null

    //当前页数
    var currentPage: Int = 1
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        mActivity = this


        //状态栏黑色
        BarUtils.setStatusBarLightMode(this, true)

        binding = ViewBindingUtil.inflateWithGeneric(this, layoutInflater)
        setContentView(binding.root)

        initData()
        observe()
        loadNetData()

    }

    /**
     * 初始化数据
     */
    abstract fun initData()
    open fun observe() {
        Bus.observe<String>(bus_finish_load, this) {
            finishRefreshAndLoadMore()
        }
    }

    /**
     *设置中心title和返回监听
     */
    open fun setTitleText(
        title: String?, textVisible: Boolean = false, bgTransparent: Boolean = false, rightTitle: String = "", click: (() -> Unit)? = null
    ) {
        findViewById<TextView>(R.id.tv_center_title).text = title
        findViewById<View>(R.id.btn_back).setOnClickListener { finish() }
        findViewById<View>(R.id.backText).visibility = if (textVisible) View.VISIBLE else View.INVISIBLE
        findViewById<TextView>(R.id.tv_right_title).text = rightTitle
        findViewById<View>(R.id.tv_right_title).setOnClickListener { click?.invoke() }
        if (bgTransparent) {
            findViewById<View>(R.id.toolbar_layout).setBackgroundColor(Color.TRANSPARENT)
        }

    }


    fun showLoading() {
//        loadingView?.takeIf { it.isShowing }?.let {
//            // 已经在loading 不重复展示
//            return
//        }
//        loadingView = showLoadingDialog(this)
//        loadingView?.show()
    }

    fun dismissLoading() {
        loadingView?.dismiss()
        loadingView = null
    }


    open fun loadNetData() {

    }

    /**
     * 刷新和加载更多
     */
    open fun setOnRefreshLoadMore(baseSmartRefreshLayout: SmartRefreshLayout?) {
        this.baseSmartRefreshLayout = baseSmartRefreshLayout
        baseSmartRefreshLayout?.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onLoadMore(refreshLayout: RefreshLayout) {
                currentPage++
                loadNetData()
            }

            override fun onRefresh(refreshLayout: RefreshLayout) {
                currentPage = 1
                loadNetData()
            }
        })
    }

    /*** 刷新和加载更多完成*/
    fun finishRefreshAndLoadMore() {
        baseSmartRefreshLayout?.finishRefresh()
        baseSmartRefreshLayout?.finishLoadMore()
    }

    open fun isActivityOnTop(activity: Activity): Boolean {
        val am = activity.getSystemService(ACTIVITY_SERVICE) as ActivityManager
        val taskInfo = am.getRunningTasks(1)
        if (taskInfo.isNotEmpty()) {
            val topActivity = taskInfo[0].topActivity
            if (topActivity!!.className == activity.javaClass.name) {
                return true
            }
        }
        return false
    }
}